package com.application.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.application.Controller.ActivityController;
import com.application.Controller.ReadWriteFileController;
import com.application.DTO.RoleDTO;


public class frmQuanLyRoleActivity extends Activity {
	final int MY_REQUEST_TAO_ROLE = 100;
	final int MY_REQUEST_SAVE_FILE = 101;
	final int MY_REQUEST_LOAD_FILE = 102;
	ListView lvDanhSachVaiTro;
	ArrayList<RoleDTO> listRole = null;
	String path = ReadWriteFileController.ROLE_SETTING_PATH;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("phu", "onCreate");
		setContentView(R.layout.layout_quan_ly_role);
		lvDanhSachVaiTro = (ListView) findViewById(R.id.lvDanhSachVaiTro);
		fillListView();
		registerForContextMenu(lvDanhSachVaiTro);

		// this.setTitle(NetworkController._peer.getAddressPeer());
	}

	void fillListView() {

		listRole = ReadWriteFileController.readRole(null,path);
		if(listRole==null){
			lvDanhSachVaiTro.setAdapter(null);
			return;
		}
		ArrayAdapter<RoleDTO> adapter = new ArrayAdapter<RoleDTO>(this, android.R.layout.simple_list_item_1, listRole);
		lvDanhSachVaiTro.setAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(getApplication()).inflate(R.menu.omnu_quan_ly_vai_tro, menu);
		return (super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.mnuThemMoi:
			Intent intent = new Intent(getBaseContext(), frmTaoRoleActivity.class);
			startActivityForResult(intent, MY_REQUEST_TAO_ROLE);
			break;
		case R.id.mnuLuaChon:
			Intent intent2=new Intent(getBaseContext(),frmDanhSachVaiTroMacDinh.class);
			startActivity(intent2);
			break;
		case R.id.mnuSave:
			Intent fileDialog = new Intent(getBaseContext(), FrmSelectFile.class);
			startActivityForResult(fileDialog, MY_REQUEST_SAVE_FILE);
			break;
		case R.id.mnuLoadFile:
			Intent fileDialogLoad = new Intent(getBaseContext(), FrmSelectFile.class);
			startActivityForResult(fileDialogLoad, MY_REQUEST_LOAD_FILE);
			break;
		default:
			break;
		}
		return true;
	}

	public void showToast(String message) {
		Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG);
		t.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == MY_REQUEST_TAO_ROLE) {
			if (resultCode == RESULT_OK) {
				Bundle b = data.getExtras();
				RoleDTO role = (RoleDTO) b.getSerializable("Role");
				listRole.add(role);
			}
		} else if (requestCode == MY_REQUEST_SAVE_FILE) {
			Bundle b = data.getExtras();
			if (b != null) {
				String path = b.getString(FrmSelectFile.RESULT_PATH);
				ReadWriteFileController.writeRole(path, listRole);
				showToast("Đã lưu xuống tập tin: " + path);
			}
		}else if(requestCode==MY_REQUEST_LOAD_FILE){
			Bundle b = data.getExtras();
			String currentPath=b.getString(FrmSelectFile.RESULT_PATH);
			if (b != null && currentPath!=null) {
				ArrayList<RoleDTO> roleList =ReadWriteFileController.readRole(null,currentPath);		
				if(roleList==null){
					showToast("Tập tin không đúng định dạng!");
					return;
				}
				ReadWriteFileController.writeRole(ReadWriteFileController.ROLE_SETTING_PATH, roleList);
				
				fillListView();
				showToast("Load từ tập tin: " + currentPath);
			}
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.lvDanhSachVaiTro) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle("Thao tác");
			menu.addSubMenu(Menu.NONE, 1, 1, "Xóa");
			menu.addSubMenu(Menu.NONE, 2, 2, "Cập nhật");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		String text = listRole.get(info.position).getName();
		if (menuItemIndex == 1) {
			ReadWriteFileController.deleteRole(path, listRole.get(info.position));
			fillListView();
		} else if (menuItemIndex == 2) {
			Intent intent = new Intent(this, frmTaoRoleActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("Update", listRole.get(info.position));
			intent.putExtras(bundle);
			startActivity(intent);
		}
		return true;
	}

	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d("phu", "onStop");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d("phu", "onRestart");
	}

}
